package com.ruoyi.edu.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.edu.domain.vo.EduCourseVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edu.domain.vo.EduCourseCollectVo;
import com.ruoyi.edu.domain.bo.EduCourseCollectBo;
import com.ruoyi.edu.service.IEduCourseCollectService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程收藏
 *
 * @author ssl
 * @date 2022-08-31
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/edu/collection")
public class EduCourseCollectController extends BaseController {

    private final IEduCourseCollectService iEduCourseCollectService;


    /**
     * 查询课程收藏列表
     */
    @SaCheckPermission("edu:collection:list")
    @GetMapping("/list")
    public TableDataInfo<EduCourseVo> list(EduCourseCollectBo bo, PageQuery pageQuery) {
        return iEduCourseCollectService.queryPageList(bo, pageQuery);

    }

    /**
     * 导出课程收藏列表
     */
    @SaCheckPermission("edu:collection:export")
    @Log(title = "课程收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EduCourseCollectBo bo, HttpServletResponse response) {
        List<EduCourseCollectVo> list = iEduCourseCollectService.queryList(bo);
        ExcelUtil.exportExcel(list, "课程收藏", EduCourseCollectVo.class, response);
    }

    /**
     * 获取课程收藏详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("edu:collection:query")
    @GetMapping("/{id}")
    public R<EduCourseCollectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iEduCourseCollectService.queryById(id));
    }

//    @SaCheckPermission("edu:collection:collect")
    @Anonymous
    @GetMapping("/collect/{id}")
    public R<List<EduCourseVo>> getCollection(@NotNull(message = "主键不能为空")
                                         @PathVariable String id) {
        List<EduCourseVo> list = iEduCourseCollectService.getEduCourseByMemberId(id);
        return R.ok(list);
    }

    /**
     * 新增课程收藏
     */
    @SaCheckPermission("edu:collection:add")
    @Log(title = "课程收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EduCourseCollectBo bo) {
        return toAjax(iEduCourseCollectService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改课程收藏
     */
    @SaCheckPermission("edu:collection:edit")
    @Log(title = "课程收藏", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EduCourseCollectBo bo) {
        return toAjax(iEduCourseCollectService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除课程收藏
     *
     * @param ids 主键串
     */
    @SaCheckPermission("edu:collection:remove")
    @Log(title = "课程收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iEduCourseCollectService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
